package com.example.teacherservice.repository;

import com.example.teacherservice.dto.PracticeDetailDTO;
import com.example.teacherservice.entity.Practice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface PracticeRepository extends JpaRepository<Practice, Long> {

    // 根据教师ID和状态查询实践活动
    List<Practice> findByTeaIdAndStatus(Long teaId, Integer status);

    // 根据课程ID查询实践活动
    List<Practice> findByCouId(Long couId);

    // 使用 JOIN FETCH 避免 N+1 问题
    @Query("SELECT p FROM Practice p JOIN FETCH p.course LEFT JOIN FETCH p.studentPractices WHERE p.teaId = :teacherId")
    List<Practice> findByTeaId(@Param("teacherId") Long teacherId);

    // 统计教师创建的实践活动数量
    @Query("SELECT COUNT(p) FROM Practice p WHERE p.teaId = ?1")
    long countByTeacherId(Long teaId);

    // 查询即将开始的实践活动（未来7天内）
    @Query("SELECT p FROM Practice p WHERE p.startTime BETWEEN CURRENT_TIMESTAMP AND CURRENT_TIMESTAMP + 7 DAY")
    List<Practice> findUpcomingPractices();

    List<Practice> findByDeleted(int deleted);



    // 方法2：返回实体后再转换（备选）
    Optional<Practice> findFullPracticeById(Long id);
}